package com.baturu.vin.hyundai.service.impl;

import com.baturu.vin.hyundai.dto.HyundaiKiaAttributeTransformDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;

@Slf4j
@Service
public class HyundaiKiaAttributeService implements InitializingBean {

    /**
     * 缓存转化处理方法
     */
    private List<HyundaiKiaAttributeTransformDTO> transformFunctions = Collections.emptyList();

    /**
     * 转化关键属性为btr字段属性
     * @param describe EPC描述信息
     * @return btr字段属性集合
     */
    public Map<String, Object> transformMajorToBtrAttribute (String describe) {
        Map<String, Object> parameter = null;
        List<HyundaiKiaAttributeTransformDTO> majorTransforms = getMajorTransforms();
        for (HyundaiKiaAttributeTransformDTO function : majorTransforms) {
            parameter = function.apply(describe);
            if (parameter != null) {
                break;
            }
        }
        return parameter;
    }

    /**
     * 获取所有有序转换器，本来提供给外部，暂时私有化
     * @return 转换器
     */
    private List<HyundaiKiaAttributeTransformDTO> getMajorTransforms () {
        return transformFunctions;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        initTransformFunction();
    }

    private void initTransformFunction () {
        List<HyundaiKiaAttributeTransformDTO> list = new ArrayList<>();
        // 引擎信息
        list.add(new HyundaiKiaAttributeTransformDTO("^([A-Z_/ ]+) - ((\\d+) SPEED|[A-Z]+) (\\dWD)$", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                Map<String, Object> parameter = new HashMap<>();

                Map<String, String> transmissionMap = getTransmissionMap(matcher.group(1));
                if (MapUtils.isEmpty(transmissionMap)) {
                    return null;
                }
                parameter.putAll(transmissionMap);

                String gearNumGroup = matcher.group(2);
                if (gearNumGroup.contains("SPEED")) {
                    parameter.put("gearsNum", matcher.group(3) + "档");
                }

                String driveType = "";
                switch (matcher.group(4)) {
                    case "2WD":
                        driveType = "两驱";
                        break;
                    case "4WD":
                        driveType = "四驱";
                        break;
                }
                if (StringUtils.isNotBlank(driveType)) {
                    parameter.put("driveType", driveType);
                }

                return parameter;
            }
        }));
        list.add(new HyundaiKiaAttributeTransformDTO("^(\\d+) SPEED( (AT|MT))?( (\\dWD))?$", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                Map<String, Object> parameter = new HashMap<>();

                String gearsNum = matcher.group(1);
                parameter.put("gearsNum", gearsNum + "档");

                String transmissionGroup = matcher.group(2);
                if (transmissionGroup != null) {
                    Map<String, String> transmissionMap = getTransmissionMap(matcher.group(3));
                    parameter.putAll(transmissionMap);
                }

                String driveTypeGroup = matcher.group(4);
                if (driveTypeGroup != null) {
                    String driveType;
                    switch (matcher.group(5)) {
                        case "2WD":
                            driveType = "两驱";
                            break;
                        case "4WD":
                            driveType = "四驱";
                            break;
                        default:
                            return null;
                    }
                    parameter.put("driveType", driveType);
                }

                return parameter;
            }
        }));
        list.add(new HyundaiKiaAttributeTransformDTO("^(AUTO|MANUAL) TRANSAXLE", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                Map<String, Object> parameter = new HashMap<>();
                if ("AUTO".equals(matcher.group(1))) {
                    parameter.put("transmissionType", "自动");
                    parameter.put("transmissionDesc", "自动变速器(AT)");
                } else {
                    parameter.put("transmissionType", "手动");
                    parameter.put("transmissionDesc", "手动变速器(MT)");
                }
                return parameter;
            }
        }));
        list.add(new HyundaiKiaAttributeTransformDTO("^CVT$", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                Map<String, Object> parameter = new HashMap<>();
                parameter.put("transmissionType", "");
                parameter.put("transmissionDesc", "无级变速器(CVT)");
                return parameter;
            }
        }));
        // 排量信息
        list.add(new HyundaiKiaAttributeTransformDTO("^(\\d+)( )?CC( .+)?$", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                DecimalFormat decimalFormat = new DecimalFormat("##0.0");
                String displacement = decimalFormat.format(Float.valueOf(matcher.group(1))/1000.0);
                Map<String, Object> parameter = new HashMap<>();
                parameter.put("displacement", displacement);
                return parameter;
            }
        }));
        // 车款、车门、车座信息
        String vehicleTypeRegex = "WAGON|SEDAN|COUPE|VAN";
        list.add(new HyundaiKiaAttributeTransformDTO("^((" + vehicleTypeRegex + ") - (\\d+)DR( (\\d+)P)?|(\\d+) DR (" + vehicleTypeRegex + ")( (\\d+))?)$", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                String group = matcher.group();
                int vehicleTypeIdx = group.contains("-") ? 2 : 7;
                int doorNumberIdx = group.contains("-") ? 3 : 6;
                int seatsIdx = group.contains("-") ? 4 : 8;

                Map<String, Object> parameter = new HashMap<>();
                String vehicleType = "";
                switch (matcher.group(vehicleTypeIdx)) {
                    case "WAGON":
                        vehicleType = "货车";
                        break;
                    case "SEDAN":
                        vehicleType = "轿车";
                        break;
                    case "COUPE":
                        vehicleType = "轿跑车";
                        break;
                    case "VAN":
                        vehicleType = "厢式货车";
                        break;
                }
                parameter.put("vehicleType", vehicleType);
                parameter.put("doorNumber", matcher.group(doorNumberIdx) + "门");

                String seatsGroup = matcher.group(seatsIdx);
                if (seatsGroup != null) {
                    parameter.put("seats", matcher.group(seatsIdx + 1));
                }
                return parameter;
            }
        }));
        // 车门信息
        list.add(new HyundaiKiaAttributeTransformDTO("^(\\d+) DOOR$", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                Map<String, Object> parameter = new HashMap<>();
                parameter.put("doorNumber", matcher.group(1) + "门");
                return parameter;
            }
        }));
        // 涡轮增压
        list.add(new HyundaiKiaAttributeTransformDTO("^TURBO CHARGE$", new Function<Matcher, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Matcher matcher) {
                Map<String, Object> parameter = new HashMap<>();
                parameter.put("airIntake", "涡轮增压");
                return parameter;
            }
        }));
//        // 左右肽信息
//        list.add(new HyundaiKiaAttributeTransformDTO("^(LEFT|RIGHT) HAND DRIVE$", new Function<Matcher, Map<String, Object>>() {
//            @Override
//            public Map<String, Object> apply(Matcher matcher) {
//                Map<String, Object> parameter = new HashMap<>();
//                parameter.put("???", "LEFT".equals(matcher.group(1)) ? "左肽" : "右肽");
//                return parameter;
//            }
//        }));
        transformFunctions = list;
    }

    private Map<String, String> getTransmissionMap (String transmission) {
        String transmissionType = "";
        String transmissionDesc = "";
        switch (transmission) {
            case "AUTO":
                transmissionType = "自动";
                transmissionDesc = "自动变速器(AT)";
                break;
            case "MANUAL T/M":
                transmissionType = "手动";
                transmissionDesc = "手动变速器(MT)";
                break;
            case "AT":
                transmissionType = "自动";
                transmissionDesc = "自动变速器(AT)";
                break;
            case "MT":
                transmissionType = "手动";
                transmissionDesc = "手动变速器(MT)";
                break;
            case "DCT":
                transmissionDesc = "双离合变速器(DCT)";
                break;
            case "CVT":
                transmissionDesc = "无级变速器(CVT)";
                break;
        }
        if (StringUtils.isNoneBlank(transmissionType) || StringUtils.isNoneBlank(transmissionDesc)) {
            Map<String, String> transmissionMap = new HashMap<>();
            transmissionMap.put("transmissionType", transmissionType);
            transmissionMap.put("transmissionDesc", transmissionDesc);
            return transmissionMap;
        } else {
            return Collections.emptyMap();
        }
    }
}